package com.heye.imheye.ws;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.heye.imheye.dto.UserInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;

@Slf4j
@RequiredArgsConstructor
public class MyHandler extends AbstractWebSocketHandler {

    private final WsContainer wsContainer;
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        log.info("有一个连接");
        UserInfo userInfo = (UserInfo) session.getAttributes().get("user");
        wsContainer.put(userInfo.id(), session);
    }

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        log.info("收到了一个消息" + message.getPayload());
//        WsMessage wsMessage = objectMapper.readValue(message.getPayload(), WsMessage.class);
    }
}
